const util = require('../../../utils/request')
const api = require('../../../config/api')
const app = getApp()

Page({
  data: {
    username: '',
    password: '',
  },
  login() {
    let username = this.data.username
    let password = this.data.password
    if (username === undefined || username === '') {
      util.showToast("用户名不能为空", "error", 1500)
      return;
    }
    if (password === undefined || password === '') {
      util.showToast("密码不能为空", "error", 1500)
      return;
    }
    let data = {
      username: username,
      password: password
    }
    util.showLoading("登陆中...")
    util.request(api.loginByAccount, data, "POST").then(res => {
      util.hideLoading()
      if (res.code !== undefined && res.code === 0) {
        wx.setStorageSync('userInfo', res.data)
        wx.setStorageSync('hasLogin', true)
        wx.switchTab({
          url: '/pages/index/index',
        })
        util.showToast("登录成功", "success", 1500)
      } else {
        util.showToast(res.msg, "error", 1500)
      }
    })
  },
  goRegister() {
    wx.navigateTo({
      url: '/pages/auth/register/register',
    })
  },

  onLoad() {

  },

})
